﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Things.Common;

namespace Things.Web.Components
{
    public partial class GoodsRelationInformation : System.Web.UI.UserControl
    {
        public Guid GoodsID
        {
            get;
            set;
        }

        public Guid GoodsPriceID
        {
            get
            {
                string temp = hfGoodsPriceID.Value;
                Guid guid = Guid.Empty;
                if (Guid.TryParse(temp, out guid) == false)
                {
                    guid = Guid.Empty;
                }
                return guid;
            }
            set
            {
                hfGoodsPriceID.Value = value.ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hfGoodsPriceID.Value = GoodsPriceID.ToString();

                Things.Model.Buyer buyer = GetCurrentBuyer();
                Things.BLL.GoodsOrders goBLL=new BLL.GoodsOrders();

                BindGoodsDetail();
                BindGoodsSourceInformation();
                BindGoodsComments();
                BindGoodsSaleRecord();

                Things.BLL.GoodsComments gcBLL = new BLL.GoodsComments();
                
                // 当前用户已注册，购买过商品，订单状态==已签收，并且没评论过的用户可以进行评论
                if (buyer != null
                    && goBLL.GetAllSuccessfulOrder(buyer.Account, GoodsPriceID) > gcBLL.GetAllSuccessfulComments(GoodsPriceID, buyer.Account) )
                {
                    GoodsCommentsAdd1.Visible = true;
                }
                else
                {
                    GoodsCommentsAdd1.Visible = false;
                }

                //GoodsCommentsAdd1.Visible = true;
            }
        }

        protected void BindGoodsDetail()
        {
            Things.BLL.Goods gBLL = new BLL.Goods();
            Things.Model.Goods gModle = gBLL.GetModel(GoodsID);
            if (gModle != null)
            {
                lblBaoZhiQi.Text = gModle.ShelfLife;
                lblChanPinBiaoZhunHao.Text = gModle.ChanPinBiaoZhunHao;
                lblChuChangFangFa.Text = gModle.ChuCangFangFa;
                lblDescription.Text = gModle.Description;
                lblJingHanLiang.Text = gModle.GoodsWeight;
                lblShengChanRiQi.Text = GetCropsInfo(gModle.ProductID)[1];
                lblShengChanXuKeZhengBianHao.Text = gModle.ShengChanXuKeZhengBianHao;
                lblYuanChanDi.Text = GetCropsInfo(gModle.ProductID)[0];

                Trade1.TradeCode = gModle.ShengChanPiCi;
                GoodsComments1.GoodsPriceID = GoodsPriceID;
                GoodsCommentsAdd1.GoodsPriceID = GoodsPriceID;
                
            }
        }

        protected List<string> GetCropsInfo(Guid productID)
        {
            List<string> temp = new List<string>();
            Things.BLL.CropsProduct cpBLL = new BLL.CropsProduct();
            DataSet ds = cpBLL.GetCropsInfo(productID);
            if (ds != null && ds.Tables.Count > 0)
            {
                DataTable dt = ds.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < dt.Columns.Count; i++)
                    {
                        string info = ConvertColumn(dt.Rows[0][i]);
                        temp.Add(info);
                    }
                }
            }           

            return temp;
        }

        protected string ConvertColumn(object obj)
        {
            string temp = string.Empty;

            if(obj!=null)
            {
                try
                {
                    DateTime result = DateTime.MinValue;
                    if (DateTime.TryParse(obj.ToString(), out result) == true)
                    {
                        temp = result.ToString("yyyy-MM-dd");
                    }
                    else
                    {
                        temp = obj.ToString();
                    }
                }
                catch
                {
                    temp = string.Empty;
                }
            }

            return temp;
        }

        protected void BindGoodsSourceInformation()
        {
        }

        protected void BindGoodsComments()
        {
        }

        protected void BindGoodsSaleRecord()
        {
            SaleList1.GoodsPriceID = GoodsPriceID;
        }

        protected Things.Model.Buyer GetCurrentBuyer()
        {
            Things.Model.Buyer buyer = null;
            if (Session[ConstantString.SessionCurrentBuyerAccount] == null)
            {
                buyer = null;
            }
            else
            {
                buyer = Session[ConstantString.SessionCurrentBuyerAccount] as Things.Model.Buyer;
            }
            return buyer;
        }
    }
}